package ebusiness.ebusiness.Dao.UserDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Entity.UserEntity.Administrator;
import ebusiness.ebusiness.Entity.UserEntity.User;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminQueryVO {
    public List<User> getUserByID() throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            AdminQuery adminQuery=sqlSession.getMapper(AdminQuery.class);
            List<User> userList=adminQuery.getUserByID();
            sqlSession.close();
            return userList;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;

    }

    public int addAUser(String customer_name,String customer_password)throws  Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            AdminQuery adminQuery = sqlSession.getMapper(AdminQuery.class);
            adminQuery.addAUser( customer_name, customer_password);
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }
        catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
            return 0;
        }

    }

    public int modifyUser(int customer_id,String customer_name,String customer_password) throws  Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            AdminQuery adminQuery = sqlSession.getMapper(AdminQuery.class);
            adminQuery.modifyUser(customer_id, customer_name, customer_password);
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }
        catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
            return 0;
        }

    }

    public int deleteUser(int uid) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            AdminQuery adminQuery = sqlSession.getMapper(AdminQuery.class);
            adminQuery.deleteUser(uid);
            sqlSession.commit();
            sqlSession.close();
            return 1;
        }
        catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
            return 0;
        }

    }
    public Administrator getAdminByName(String name) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            AdminQuery adminQuery = sqlSession.getMapper(AdminQuery.class);
            Administrator administrator=adminQuery.findAdminByName(name);
            sqlSession.close();
            return administrator;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;

    }

}
